package com.etone.smartAudit.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.PTodo;
import com.etone.smartAudit.dto.PortalPushDTO;
import com.etone.smartAudit.mapper.PTodoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class PTodoService extends ServiceImpl<PTodoMapper, PTodo> {
    /***
     * 系统推送id
     */
    @Value("${portal.sysId:GMCCSMARTAUDIT_ACCESSRIGHT}")
    private String sysId;

    @Value("${portal.appName:广东大数据智慧审计中台}")
    private String appName;

    @Value("${portal.password:Abc@#123}")
    private String password;

    @Value("${portal.userid:xtsgssmartaudit}")
    private String userid;

    /***
     * 创建一条代办消息
     * @param pushDTO
     */
    public PTodo createPTodoService(PortalPushDTO pushDTO) {
        PTodo pTodo = new PTodo();
        // 系统名称（或者系统id）
        pTodo.setAppName(appName);
        // 接收者portal账号
        pTodo.setOwner(pushDTO.getOwner());
        // 上一步执行人
        pTodo.setParentOwner(pushDTO.getParentOwner());
        // 代办标题
        pTodo.setTodoTitle(pushDTO.getTodoTitle());
        // 跳转链接
        pTodo.setUrl(pushDTO.getUrl());
        // 创建时间
        pTodo.setCreateTime(new Date());
        // 更新时间
        pTodo.setModifyTime(new Date());
        // 操作类型。a=增，m=改，d=删
        pTodo.setType(pushDTO.getType());

        // 推送用户id
        pTodo.setUid(userid);
        // 时间戳
        String timestamp = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
        pTodo.setTimestamp(timestamp);
        // 密码
        pTodo.setPassword(password);
        // 系统id
        pTodo.setSysid(sysId);
        // business是string业务名称，如:todo，done，toread，read
        pTodo.setBusiness(pushDTO.getBusiness());
        // 唯一流水号，只在log4j日志中记录，排查使用
        if (StrUtil.isEmpty(pushDTO.getSeqno())) {
            pTodo.setSeqno(RandomUtil.randomString(32));
        } else {
            pTodo.setSeqno(pushDTO.getSeqno());
        }
        this.save(pTodo);
        return pTodo;
    }
}
